#include "EPubBasicRootImp.h"
#include "EPubUtil.h"
#include "IEPubDocument.h"
#include "EPubPagingImp.h"

EPubBasicRootImp::EPubBasicRootImp()
	: m_Paging(new EPubPagingImp)
{
}

EPubPointer<IEPubBuffer> EPubBasicRootImp::Serialize() const
{
	return NULL;
}

EPUB_Result EPubBasicRootImp::Deserialize(const EPubPointer<IEPubBuffer> buffer)
{
	EPUB_Result ret = EPUB_SUCCESS;

	TiXmlDocument doc;
	doc.Parse((const char *)buffer->GetBuffer());

	const TiXmlElement *elem_package = NULL;
	const TiXmlElement *elem_manifest = NULL;
	const TiXmlElement *elem_spine = NULL;

	EPub_Xml_Element(&doc, "package", &elem_package);
	EPub_Xml_Element(&doc, "package>manifest", &elem_manifest);
	EPub_Xml_Element(&doc, "package>spine", &elem_spine);


	// Metadata.
	EPub_Xml_Attribute(elem_package, "version", m_Version);
	EPub_Xml_Attribute(elem_package, "prefix", m_Prefix);
	EPub_Xml_Attribute(elem_package, "xml:lang", m_Lang);


	if(EPUB_SUCCEEDED(EPub_Xml_Attribute(elem_package, "unique-identifier", m_UniqueIdentifier))) {
		const TiXmlElement *elem_identifier = NULL;
		if(EPUB_SUCCEEDED(EPub_Xml_Element(elem_package, "metadata>dc:identifier", std::string("id:") + m_UniqueIdentifier, &elem_identifier))) {
			EPub_Xml_Text(elem_identifier, m_UniqueIdentifier);
		}
	}
	
	EPub_Xml_Text(elem_package, "metadata>dc:title", m_Title);
	EPub_Xml_Text(elem_package, "metadata>dc:language", m_Language);

	EPub_Xml_Text(elem_package, "metadata>dc:contributor", m_Contributor);
	EPub_Xml_Text(elem_package, "metadata>dc:coverage", m_Coverage);
	EPub_Xml_Text(elem_package, "metadata>dc:creator", m_Creator);
	EPub_Xml_Text(elem_package, "metadata>dc:date", m_Date);
	EPub_Xml_Text(elem_package, "metadata>dc:description", m_Description);
	EPub_Xml_Text(elem_package, "metadata>dc:format", m_Format);
	EPub_Xml_Text(elem_package, "metadata>dc:publisher", m_Publisher);
	EPub_Xml_Text(elem_package, "metadata>dc:relation", m_Relation);
	EPub_Xml_Text(elem_package, "metadata>dc:rights", m_Rights);
	EPub_Xml_Text(elem_package, "metadata>dc:source", m_Source);
	EPub_Xml_Text(elem_package, "metadata>dc:subject", m_Subject);
	EPub_Xml_Text(elem_package, "metadata>dc:type", m_Type);

	// Manifest.
	for(const TiXmlElement *elem_item = elem_manifest->FirstChildElement("item");
		elem_item;
		elem_item = elem_item->NextSiblingElement("item"))
	{
		std::string id, href, media_type;
		EPub_Xml_Attribute(elem_item, "id", id);
		EPub_Xml_Attribute(elem_item, "href", href);
		EPub_Xml_Attribute(elem_item, "media-type", media_type);

		EPUB_MediaFileType file_type = IEPubDocument::ToMediaFileType(media_type.c_str());
		EPubPointer<IEPubMedia> media_file = IEPubDocument::CreateMediaFile(file_type);

		if(!media_file) {
			continue;
		}

		media_file->SetHref(href.c_str());
		m_ManifestList[id] = media_file;
	}

	// Spine.
	for(const TiXmlElement *elem_item = elem_spine->FirstChildElement("itemref");
		elem_item;
		elem_item = elem_item->NextSiblingElement("itemref"))
	{
		std::string idref;
		EPub_Xml_Attribute(elem_item, "idref", idref);

		EPubPointer<IEPubMedia> media_file = m_ManifestList[idref];
		m_Paging->AddPage(media_file);
	}

	return ret;
}